package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-24
 * Time: 13:52
 */
class SingletonLazy{
    private static Object locker1 = new Object();
    private static volatile SingletonLazy instance = null;
    public static SingletonLazy getInstance(){
        if (instance == null){
            synchronized(locker1){
                if (instance == null){   //如果instance为null，则进入if创建实例，创建实例的时机是第一次调用getInstance这个方法
                    instance = new SingletonLazy();
                }
            }
        }

        return instance;//第二次第三次调用getInstance()这个方法，就不会进入if语句中，而直接返回instance了
    }

    private SingletonLazy(){

    }
}
public class ThreadDemon29 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }

}
